﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Constants;
using mshtml;
using BotvaBot.Configuration;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class LookingForMonsterChainItem : BotvaLogedInChainItemBase
    {
        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru/dozor.php";
            Method = HttpMethod.POST;
            AddGet("a", "monster");

            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;

            foreach (IHTMLElement item in all)
            {
                if (item.tagName.ToUpper() != "FORM")
                    continue;

                if (item.getAttribute("action", 0).ToString().Trim().ToUpper() != "?a=monster".Trim().ToUpper())
                    continue;

                #region
                //IHTMLElementCollection inputs = (item.children as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;

                //bool isFormCorrect = false;
                
                //foreach (IHTMLElement input in inputs)
                //{
                //    if(!(input is IHTMLInputElement))
                //        continue;
                    
                //    IHTMLInputElement inp = input as IHTMLInputElement;
                //    if (inp.type.Trim().ToUpper() == "SUBMIT" && inp.value.Trim() == "ОТПРАВИТЬ" && input.className.Trim() == "CMD_ALL CMD_ROW4 CMD_AROW4")
                //    {
                //        isFormCorrect = true;
                //    }

                //    if (isFormCorrect)
                //        break;
                //}

                //if (!isFormCorrect)
                //    continue;
                #endregion

                IHTMLElementCollection inputs = (item.children as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;
                List<IHTMLInputElement> inps = new List<IHTMLInputElement>(inputs.Cast<IHTMLInputElement>());

                #region
                //isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "K").Count() > 0;
                //isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "BLOCK").Count() > 0;
                //isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "DO_CMD").Count() > 0;
                //isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "FLYING").Count() > 0;

                //if (!isFormCorrect)
                //    continue;
                #endregion

                bool isOk = false;

                foreach (IHTMLElement input in inputs)
                {
                    IHTMLInputElement inp = input as IHTMLInputElement;

                    if (inp.name != null && inp.name.Trim().ToUpper() == "K")
                    {
                        AddPost("k", input.getAttribute("VALUE", 0).ToString().ToLower());
                        isOk = true;
                        break;
                    }
                }

                if (isOk)
                    break;
            }

            AddPost("ptype", "1");
            AddPost("level", Config.Custom.MonsterFightLevel.ToString());
        }
    }
}

/*
<form action="?a=monster" method="POST">
			<input type="hidden" value="13985" name="k">
			Цена: 
			
			<input type="radio" value="1" name="ptype">
				<span class="price_num price_num_crystal">1</span>
				<b title="Кристаллы" class="icon money2"></b> /
			<input type="radio" value="2" name="ptype">
				<span class="price_num price_num_green">1</span>  
				<b title="Зелень" class="icon money3"></b><br><br>
			
			<select id="level" class="field select_level " name="level">
				<option value="1">страшилка 1 уровня</option>
				<option value="2">страшилка 2 уровня</option>
				<option selected="" value="3">страшилка 3 уровня</option>
				<option value="4">бандюк 1-3 уровня</option>
			</select><br><br>
			
			<input type="submit" value="ИСКАТЬ" class="cmd_all cmd_row3 cmd_arow3 "><br>
</form>
			
POST /dozor.php?a=monster HTTP/1.1
Host: g3.botva.ru
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20100101 Firefox/6.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*_/*;q=0.8
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://g3.botva.ru/dozor.php
Cookie: server=3; PHPSESSID=7do07g1v8m8hm0ts43ksant7l5; uid=133096

k	13985
level	3
ptype	1
*/